package com.flyco.modules.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.system.entity.SysUser;
import com.flyco.modules.system.entity.SysUserRelation;
import com.flyco.modules.system.service.ISysUserRelationService;

import java.util.Date;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 人员关系
 * @Author: flyco
 * @Date: 2023-07-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "人员关系")
@RestController
@RequestMapping("/f/system/sysUserRelation")
public class SysUserRelationController extends BaseController<SysUserRelation, ISysUserRelationService> {
    @Autowired
    private ISysUserRelationService sysUserRelationService;

    /**
     * 分页列表查询
     *
     * @param sysUserRelation
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "人员关系-分页列表查询")
    @ApiOperation(value = "人员关系-分页列表查询", notes = "人员关系-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SysUserRelation sysUserRelation,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<SysUserRelation> queryWrapper = QueryGenerator.initQueryWrapper(sysUserRelation, req.getParameterMap());
        Page<SysUserRelation> page = new Page<SysUserRelation>(pageNo, pageSize);
        IPage<SysUserRelation> pageList = sysUserRelationService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    /**
     * 查询下属列表
     */
    @AutoLog(value = "人员关系-查询下属列表")
    @ApiOperation(value = "人员关系-查询下属列表", notes = "人员关系-查询下属列表")
    @GetMapping(value = "/getByParentUserId")
    public Result<?> getByParentUserId(Long parentUserId) {
        List<SysUser> userList = sysUserRelationService.getByParentUserId(parentUserId);
        return Result.ok(userList);
    }

    /**
     * 查询下属列表
     */
    @AutoLog(value = "人员关系-查询下属列表")
    @ApiOperation(value = "人员关系-查询下属列表", notes = "人员关系-查询下属列表")
    @GetMapping(value = "/getRelationByParentUserId")
    public Result<?> getRelationByParentUserId(Long parentUserId) {
        List<SysUserRelation> userRelationList = sysUserRelationService.getRelationByParentUserId(parentUserId);
        if (!CollectionUtils.isEmpty(userRelationList)) {
            List<Long> userIdList = userRelationList.stream().map(o -> o.getUserid()).collect(Collectors.toList());
            return Result.ok(userIdList);
        }
        return Result.ok("");
    }

    /**
     * 添加
     *
     * @param sysUserRelation
     * @return
     */
    @AutoLog(value = "人员关系-添加")
    @ApiOperation(value = "人员关系-添加", notes = "人员关系-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysUserRelation sysUserRelation) {
        sysUserRelationService.save(sysUserRelation);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysUserRelation
     * @return
     */
    @AutoLog(value = "人员关系-编辑")
    @ApiOperation(value = "人员关系-编辑", notes = "人员关系-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysUserRelation sysUserRelation) {
        sysUserRelationService.updateById(sysUserRelation);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "人员关系-通过id删除")
    @ApiOperation(value = "人员关系-通过id删除", notes = "人员关系-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        sysUserRelationService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "人员关系-批量删除")
    @ApiOperation(value = "人员关系-批量删除", notes = "人员关系-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysUserRelationService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "人员关系-通过id查询")
    @ApiOperation(value = "人员关系-通过id查询", notes = "人员关系-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysUserRelation sysUserRelation = sysUserRelationService.getById(id);
        return Result.ok(sysUserRelation);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sysUserRelation
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysUserRelation sysUserRelation) {
        return super.exportXls(request, sysUserRelation, SysUserRelation.class, "人员关系");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysUserRelation.class);
    }

}
